<?php

namespace App\Jobs;

use App\Address;
use App\Order;
use App\OrderDetail;
use App\Services\cityService;
use App\Share;
use App\Sku;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class GivingJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $order;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($order)
    {
        $this->order = $order;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $order = $this->order;
        $count = Share::where('share_uid', $order->share_uid)->where('status', 1)->count();
        $num = (int)($count / 3);
        $given = Order::where('uid', $order->share_uid)->where('type', 1)->count();
        Log::info("giving",[$num-$given]);
//        $has_buy=Order::where('share_uid',$order->share_uid)->where('status',1)->count();
        if (($num - $given) > 0) {
            $giving_num = $num - $given;
            $order_number = Carbon::now()->format('YmdHis') . random_int(1000, 9999) . $order->share_uid;
            $order2 = Order::with('detail')->where('uid', $order->share_uid)->where('status',1)->first();
            Log::info("order",[$order2]);
            $order1 = [
                'uid' => $order->share_uid,
                'order_number' => $order_number,
                'order_amount' => 0,
                'pay_amount' => 0,
                'coupon_id' => 0,
                'coupon_discount' => 0,
                'address_province' => $order2["address_province"],
                'address_city' => $order2["address_city"],
                'address_area' => $order2["address_area"],
                'address_consignee' => $order2["address_consignee"],
                'address_phone_number' =>$order2["address_phone_number"],
                'address_detail' => $order2["address_detail"],
                'deduction_wallet_money' => 0,
                'deduction_wallet' => 0,
                'quantity' => 0,
                'is_group_buy' => 0,
                'share_uid' => 0,
                'group_item_id' => 0,
                'type' => 1,
                'status' => 1
            ];
            $create = Order::create($order1);

            $detail=$order2['detail'][0];
            $detail_sku=explode(",",$detail['sku']);
            $sku="#ebc9b5浅藕粉,".$detail_sku[1];
            $sku_find=Sku::where('spec_text',$sku)->where('goods_id',9)->first();
            $detail = [
                'order_id' => $order_number,
                'goods_name' => $detail['goods_name'],
                'goods_id' => $detail['goods_id'],
                'cover' => $detail['cover'],
                'price' => $detail['price'],
                'number' => $giving_num,
                'sku_id' => $sku_find['id'],
                'sku' => $sku,
                'type' => $detail['type'],
                'origin_price' => ceil($detail['origin_price'])
            ];
            OrderDetail::create($detail);

        }
    }
}
